package com.copa.l215;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author copa
 * @createDate 2022-05-29 18:30
 * @function 堆排序法（最大堆）
 */
public class Test4 {

    public static class MaxHeapComparator implements Comparator<Integer> {
        @Override
        public int compare(Integer o1, Integer o2) {
            // 改写成大根堆排序
            return o2 - o1;
        }
    }

    public int findKthLargest(int[] nums, int k) {
        PriorityQueue<Integer> pq = new PriorityQueue<>(new MaxHeapComparator());
        for (int num : nums) {
            pq.add(num);
        }
        for (int i = 0; i < k - 1; i++) {
            pq.poll();
        }
        return pq.peek();
    }

    public static void main(String[] args) {
        int[] nums = {3,2,1,5,6,4};
        System.out.println(new Test4().findKthLargest(nums, 2));
    }
}
